package fy.hr.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONObject;

import com.soa.eis.adapter.framework.message.IMsgObject;
import com.soa.eis.adapter.framework.message.impl.GroupRecord;
import com.soa.eis.adapter.framework.message.impl.MsgObject;

import fy.config.FyConfig;
import fy.util.DBUtil;
import nc.ws.impl.MYWSServiceRequester;


public class FindUserAndLeaderInfoDao {

	private static Log log = LogFactory.getLog(FindUserAndLeaderInfoDao.class);

	private String userCode;

//	private String leaderCode;
//
//	private String leaderName;
//
//	private String leaderID;

	private String post;
	
	private String deptname;
	
	private String unitshortname;

	//private static final String queryFromHRSQL = "select j.jobname, c.unitshortname, d.deptname from bd_psndoc p left join om_job j on p.pk_om_job = j.pk_om_job left join  bd_corp c on p.pk_corp = c.pk_corp left join bd_deptdoc d on p.pk_deptdoc = d.pk_deptdoc where p.psnclscope='0' and p.psncode=?";

	// private static final String queryFromHRSQL =
	// "select tab3.psncode,tab3.psnname,tab4.jobname from bd_psndoc tab1 ,bd_deptdoc tab2 , bd_psndoc tab3,om_job tab4 where tab1.psncode=? and tab1.pk_deptdoc=tab2.pk_deptdoc and tab1.pk_om_job=tab4.pk_om_job and tab3.psncode<>? and ( tab2.pk_psndoc=tab3.pk_psndoc or tab2.pk_psndoc2=tab3.pk_psndoc or tab2.pk_psndoc3=tab3.pk_psndoc)";

//	private static final String queryFromOASQL = "select v.id from v3x_org_member v where v.code =?";

	public void setUserCode(String userCode) {
		this.userCode = userCode;
	}

	public String getPost() {
		return post;
	}
	
	public String getDeptname() {
		return deptname;
	}

	public String getUnitshortname() {
		return unitshortname;
	}

//	public String getLeaderName() {
//		return leaderName;
//	}
//
//	public String getLeaderID() {
//		return leaderID;
//	}

public void query() {

		queryFromHR();

//		if (leaderCode != null && !"".equals(leaderCode)) {
//			queryFromOA();
//		}
	}
	private void queryFromHR() {
	/*	Connection conn = null;
		PreparedStatement st = null;
		ResultSet rs = null;
		try {
			conn = DBUtil.connect(FyConfig.getEHR_DB_CONFIG());

			st = conn.prepareStatement(queryFromHRSQL);
			st.setString(1, userCode);
			// st.setString(2, userCode);

			rs = st.executeQuery();
			if (rs.next()) {
				// this.leaderCode = rs.getString(1);
				// this.leaderName = rs.getString(2);
				//
				// this.post = rs.getString(3);
				this.post = rs.getString(1);
				this.unitshortname = rs.getString(2);
				this.deptname = rs.getString(3);
			}

		} catch (Exception e) {
			log.error("个人发展计划_HR查找人员信息出错", e);
			e.printStackTrace();
		} finally {
			DBUtil.close(rs);
			DBUtil.close(st);
			DBUtil.close(conn);
		}*/
		System.out.println(">>>>>>>>>>>>");
		String mil=String.valueOf(System.currentTimeMillis());
		String sHttpPost = "http://10.112.0.206:7080/httpservice";
		IMsgObject reqMo;
		try {
			reqMo = new MsgObject(IMsgObject.MOType.initSR);
			String SourceSysID = "06002";              //请求方(发布方) 系统号    OA  
			String ServiceID = "06003000000001"; //响应方(发布方) 服务号    HR
			//获取系统时间
			Date date = new Date();
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
			String format = simpleDateFormat.format(date);
			reqMo.setSourceSysID(SourceSysID); //设置报文 请求方系统号      
			reqMo.setServiceID(ServiceID);     //设置报文 提供方服务号
			
			reqMo.setSerialNO(format.substring(0, 8)+SourceSysID+System.currentTimeMillis());//设置报文流水号,第三段表示今天发送的第几的一次报文
			reqMo.setServiceDateTime(format);  //设置报文 日期时间
			reqMo.setReqValue("string", "userCode");
			//reqMo.setReqValue("string", "084655");
			IMsgObject respMo = null;	
			log.info("reqMo:" + reqMo.toString());
			//直接访问WSDL调用方法
			MYWSServiceRequester mt = new MYWSServiceRequester(sHttpPost);
			System.out.println("??????????????");
			respMo = mt.execute(reqMo);
			log.info(">>>>>>>>>>>>>>>"+respMo);
			System.out.println(">>>>>>>>>>>>>>>"+respMo);
			List<GroupRecord> record2 = respMo.getResGroupRecord("return") ;
			JSONObject jo=new JSONObject();
			for (GroupRecord groupRecord : record2) {
				System.out.println("??????????????"+groupRecord.getFieldValue("jobname"));
			
			this.post = groupRecord.getFieldValue("jobname");
			this.unitshortname = groupRecord.getFieldValue("unitname");
			this.deptname = groupRecord.getFieldValue("deptname");
			
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
